package cn.wuxing.mediation.web.controller.zzd;

import cn.hutool.core.date.DateUtil;
import cn.wuxing.mediation.system.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;

@Slf4j
//@Component
public class MyScheduledTask {

    @Autowired
    private ZzdDeptController zzdDeptController;

    //    @Scheduled(cron = "0 0/5 * * * ?") // 每5分钟执行一次
//    @Scheduled(fixedRate = 5000)//5秒
//    @Scheduled(cron = "0 0 0 * * ?")//表示每天的 00:00 执行
    public void executeTask() {
        System.out.println("定时任务执行时间: " + new java.util.Date());
        // 在这里添加你的业务逻辑
        zzdDeptController.getDept();
    }

    private int dataSynchronizationFlag = 0;

//    @Scheduled(cron = "0 0 17 * * ?")
    public void updateDataSynchronization() {
        dataSynchronizationFlag = 0;
        log.info("数据同步变量已经置为0,18点同步数据");
    }


    /**
     * 数据同步：将中转表数据同步到业务表里
     */
//    @Scheduled(fixedRate = 5000)//5秒
//    @Scheduled(cron = "0 0 20 * * ?")
    public void dataSynchronization() {
        if (dataSynchronizationFlag == 0) {
            //设置已经执行
            dataSynchronizationFlag = 1;
            //1、表名 stg_hzdn_mplc_dev_hz_zz_event_source_odps_di_wxq 事件来源关联表 双非警情
            //->stg_hzdn_mplc_dev_hz_zz_event_source_odps_di_wxq_temp
            data_stg_hzdn_mplc_dev_hz_zz_event_source_odps_di_wxq();//已完成

            //2、表名 stg_mplc_hz_zz_event_di_wxq 事件总表 双非警情
            //->stg_mplc_hz_zz_event_di_wxq_temp
            data_stg_mplc_hz_zz_event_di_wxq();//已完成

            //3、表名 stg_sxfj_ygrx_xfsj_df_wxq 市信访局_阳光热线信访数据
            //->stg_sxfj_ygrx_xfsj_df_wxq_temp
            data_stg_sxfj_ygrx_xfsj_df_wxq();//已完成

            //4、表名 stg_sdsjj_dsc_da018_tab1010201_valid_old_huz_df_wxq 省信访局_信访件信息  I插入U更新D删除
            //->stg_sdsjj_dsc_da018_tab1010201_valid_old_huz_df_wxq_temp
            data_stg_sdsjj_dsc_da018_tab1010201_valid_old_huz_df_wxq();//已完成

            //5、表名 stg_dsc_huz_biz_018_visit_petition_process_df_wxq 信访事项办理记录  I插入U更新D删除
            //->stg_dsc_huz_biz_018_visit_petition_process_df_wxq_temp
            data_stg_dsc_huz_biz_018_visit_petition_process_df_wxq_temp();//已完成

            data_qrlsbj_stg_dsc_huz_biz_029_sjztj_df_wxq_temp();

            //最后
            //6、表名 stg_dsc_huz_biz_018_visit_petition_df_wxq 信访事项信息(数据量大)  I插入U更新D删除
            //->stg_dsc_huz_biz_018_visit_petition_df_wxq_temp
            data_stg_dsc_huz_biz_018_visit_petition_df_wxq_temp();//已完成
        }
    }

    @Resource
    private StgSdsjjDscDa018Tab1010201ValidOldHuzDfWxqMapper stgSdsjjDscDa018Tab1010201ValidOldHuzDfWxqMapper;

    @Resource
    private StgMplcHzZzEventDiWxqMapper stgMplcHzZzEventDiWxqMapper;

    @Resource
    private StgSxfjYgrxXfsjDfWxqMapper stgSxfjYgrxXfsjDfWxqMapper;

    @Resource
    private StgHzdnMplcDevHzZzEventSourceOdpsDiWxqMapper stgHzdnMplcDevHzZzEventSourceOdpsDiWxqMapper;

    @Resource
    private StgDscHuzBiz018VisitPetitionDfWxqMapper stgDscHuzBiz018VisitPetitionDfWxqMapper;

    @Resource
    private StgDscHuzBiz018VisitPetitionProcessDfWxqMapper stgDscHuzBiz018VisitPetitionProcessDfWxqMapper;

    @Resource
    private QrlsbjStgDscHuzBiz029SjztjDfWxqMapper qrlsbjStgDscHuzBiz029SjztjDfWxqMapper;


    private void data_qrlsbj_stg_dsc_huz_biz_029_sjztj_df_wxq_temp() {
        long transitCount = qrlsbjStgDscHuzBiz029SjztjDfWxqMapper
                .selectCount();//中转总数
        if (transitCount == 0) {//中转表无数据、无需同步
            log.info("中转表qrlsbj_stg_dsc_huz_biz_029_sjztj_df_wxq---省市回流_浙江劳动人事争议网络调解案件信息对象：{}", transitCount);
            return;
        }
        if (transitCount > 0) {//数据同步
            log.info("【定时任务】- 数据同步 - 信访件_信访事项信息 - 同步开始。时间：{}", DateUtil.now());
            StopWatch allCaseSyncCalculate = new StopWatch();
            allCaseSyncCalculate.start();
            int allCaseSyncResult = qrlsbjStgDscHuzBiz029SjztjDfWxqMapper.syncDataToAllCaseDatabase();
            allCaseSyncCalculate.stop();
            if (allCaseSyncResult == 0) {
                log.error("【定时任务】- 数据同步 - 信访件_信访事项信息 - 同步失败。耗时：{}s", allCaseSyncCalculate.getTotalTimeMillis() / 1000);
            } else {
                log.info("【定时任务】- 数据同步 - 信访件_信访事项信息 - 同步成功。耗时：{}s", allCaseSyncCalculate.getTotalTimeMillis() / 1000);
            }

            int i = qrlsbjStgDscHuzBiz029SjztjDfWxqMapper.dataSynchronization();
//            int i1 = stgDscHuzBiz018VisitPetitionDfWxqMapper.dataSynchronizationDemo();
//            log.info("表stg_dsc_huz_biz_018_visit_petition_df_wxq_bus同步{}", i == 0 ? "失败" : "成功");
            log.info("表qrlsbj_stg_dsc_huz_biz_029_sjztj_df_wxq_temp同步{}", i == 0 ? "失败" : "成功");
        }
    }

    //data_stg_dsc_huz_biz_018_visit_petition_df_wxq_bus
    public void data_stg_dsc_huz_biz_018_visit_petition_df_wxq_temp() {
        long transitCount = stgDscHuzBiz018VisitPetitionDfWxqMapper
                .selectCount();//中转总数
        if (transitCount == 0) {//中转表无数据、无需同步
            log.info("中转表stg_dsc_huz_biz_018_visit_petition_df_wxq---信访件_信访事项信息无数据：{}", transitCount);
            return;
        }
        if (transitCount > 0) {//数据同步
            int i = stgDscHuzBiz018VisitPetitionDfWxqMapper.dataSynchronization();
//            int i1 = stgDscHuzBiz018VisitPetitionDfWxqMapper.dataSynchronizationDemo();
//            log.info("表stg_dsc_huz_biz_018_visit_petition_df_wxq_bus同步{}", i == 0 ? "失败" : "成功");
            log.info("表stg_dsc_huz_biz_018_visit_petition_df_wxq_bus同步{}", i == 0 ? "失败" : "成功");


            log.info("【定时任务】- 数据同步 - 信访件_信访事项信息 - 同步开始。时间：{}", DateUtil.now());
            StopWatch allCaseSyncCalculate = new StopWatch();
            allCaseSyncCalculate.start();
            int allCaseSyncResult = stgDscHuzBiz018VisitPetitionDfWxqMapper.syncDataToAllCaseDatabase();
            allCaseSyncCalculate.stop();
            if (allCaseSyncResult == 0) {
                log.error("【定时任务】- 数据同步 - 信访件_信访事项信息 - 同步失败。耗时：{}s", allCaseSyncCalculate.getTotalTimeMillis() / 1000);
            } else {
                log.info("【定时任务】- 数据同步 - 信访件_信访事项信息 - 同步成功。耗时：{}s", allCaseSyncCalculate.getTotalTimeMillis() / 1000);
            }

        }
    }


    //data_stg_dsc_huz_biz_018_visit_petition_process_df_wxq_temp
    public void data_stg_dsc_huz_biz_018_visit_petition_process_df_wxq_temp() {
        long transitCount = stgDscHuzBiz018VisitPetitionProcessDfWxqMapper
                .selectCount();//中转总数
        if (transitCount == 0) {//中转表无数据、无需同步
            log.info("中转表stg_dsc_huz_biz_018_visit_petition_process_df_wxq---信访件_信访事项办理记录信息无数据：{}", transitCount);
            return;
        }
        if (transitCount > 0) {//数据同步
            int i = stgDscHuzBiz018VisitPetitionProcessDfWxqMapper.dataSynchronization();
            log.info("表stg_dsc_huz_biz_018_visit_petition_process_df_wxq_temp同步{}", i == 0 ? "失败" : "成功");
        }
    }


    //data_stg_sxfj_ygrx_xfsj_df_wxq_temp
    public void data_stg_sxfj_ygrx_xfsj_df_wxq() {
        long transitCount = stgSxfjYgrxXfsjDfWxqMapper
                .selectCount();//中转总数
        if (transitCount == 0) {//中转表无数据、无需同步
            log.info("中转表stg_sxfj_ygrx_xfsj_df_wxq---市信访局_阳光热线信访无数据：{}", transitCount);
            return;
        }
        if (transitCount > 0) {//数据同步
            int i = stgSxfjYgrxXfsjDfWxqMapper.dataSynchronization();
            log.info("表stg_sxfj_ygrx_xfsj_df_wxq_temp同步{}", i == 0 ? "失败" : "成功");

            log.info("【定时任务】- 数据同步 - 市信访局_阳光热线信访 - 同步开始。时间：{}", DateUtil.now());
            StopWatch allCaseSyncCalculate = new StopWatch();
            allCaseSyncCalculate.start();
            int allCaseSyncResult = stgSxfjYgrxXfsjDfWxqMapper.syncDataToAllCaseDatabase();
            allCaseSyncCalculate.stop();
            if (allCaseSyncResult == 0) {
                log.error("【定时任务】- 数据同步 - 市信访局_阳光热线信访 - 同步失败。耗时：{}s", allCaseSyncCalculate.getTotalTimeMillis() / 1000);
            } else {
                log.info("【定时任务】- 数据同步 - 市信访局_阳光热线信访 - 同步成功。耗时：{}s", allCaseSyncCalculate.getTotalTimeMillis() / 1000);
            }
        }
    }


    //->stg_hzdn_mplc_dev_hz_zz_event_source_odps_di_wxq_temp
    public void data_stg_hzdn_mplc_dev_hz_zz_event_source_odps_di_wxq() {
        long transitCount = stgHzdnMplcDevHzZzEventSourceOdpsDiWxqMapper
                .selectCount();//中转总数
        if (transitCount == 0) {//中转表无数据、无需同步
            log.info("中转表stg_hzdn_mplc_dev_hz_zz_event_source_odps_di_wxq---事件来源关联信息：{}", transitCount);
            return;
        }
        if (transitCount > 0) {//数据同步
            int i = stgHzdnMplcDevHzZzEventSourceOdpsDiWxqMapper.dataSynchronization();
            log.info("表stg_hzdn_mplc_dev_hz_zz_event_source_odps_di_wxq_temp同步{}", i == 0 ? "失败" : "成功");

        }
    }


    //->stg_mplc_hz_zz_event_di_wxq_temp
    public void data_stg_mplc_hz_zz_event_di_wxq() {
        long transitCount = stgMplcHzZzEventDiWxqMapper.selectCount();
        if (transitCount == 0) {//中转表无数据、无需同步
            log.info("中转表stg_mplc_hz_zz_event_di_wxq---事件信息总表：{}", transitCount);
            return;
        }
        if (transitCount > 0) {//数据同步
            log.info("【定时任务】- 数据同步 - 双非警情 - 同步开始。时间：{}", DateUtil.now());
            StopWatch allCaseSyncCalculate = new StopWatch();
            allCaseSyncCalculate.start();
            int allCaseSyncResult = stgMplcHzZzEventDiWxqMapper.syncDataToAllCaseDatabase();
            allCaseSyncCalculate.stop();
            if (allCaseSyncResult == 0) {
                log.error("【定时任务】- 数据同步 - 双非警情 - 同步失败。耗时：{}s", allCaseSyncCalculate.getTotalTimeMillis() / 1000);
            } else {
                log.info("【定时任务】- 数据同步 - 双非警情 - 同步成功。耗时：{}s", allCaseSyncCalculate.getTotalTimeMillis() / 1000);
            }

            int i = stgMplcHzZzEventDiWxqMapper.dataSynchronization();
            log.info("表stg_mplc_hz_zz_event_di_wxq_temp同步{}", i == 0 ? "失败" : "成功");
        }
    }


    //同步stg_sdsjj_dsc_da018_tab1010201_valid_old_huz_df_wxq -> stg_sdsjj_dsc_da018_tab1010201_valid_old_huz_df_wxq_temp
    public void data_stg_sdsjj_dsc_da018_tab1010201_valid_old_huz_df_wxq() {
        long transitCount = stgSdsjjDscDa018Tab1010201ValidOldHuzDfWxqMapper
                .selectCount();//中转总数
        if (transitCount == 0) {//中转表无数据、无需同步
            log.info("中转表stg_sdsjj_dsc_da018_tab1010201_valid_old_huz_df_wxq---省信访局_信访件信息无数据：{}", transitCount);
            return;
        }
        if (transitCount > 0) {//数据同步
            log.info("【定时任务】- 数据同步 - 省信访局_信访件信息 - 同步开始。时间：{}", DateUtil.now());
            StopWatch allCaseSyncCalculate = new StopWatch();
            allCaseSyncCalculate.start();
            int allCaseSyncResult = stgSdsjjDscDa018Tab1010201ValidOldHuzDfWxqMapper.syncDataToAllCaseDatabase();
            allCaseSyncCalculate.stop();
            if (allCaseSyncResult == 0) {
                log.error("【定时任务】- 数据同步 - 省信访局_信访件信息 - 同步失败。耗时：{}s", allCaseSyncCalculate.getTotalTimeMillis() / 1000);
            } else {
                log.info("【定时任务】- 数据同步 - 省信访局_信访件信息 - 同步成功。耗时：{}s", allCaseSyncCalculate.getTotalTimeMillis() / 1000);
            }

            int i = stgSdsjjDscDa018Tab1010201ValidOldHuzDfWxqMapper.dataSynchronization();
            log.info("表stg_sdsjj_dsc_da018_tab1010201_valid_old_huz_df_wxq_temp同步{}", i == 0 ? "失败" : "成功");
        }
    }
}
